package com.qq.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qq.bean.Chatrecord;
import com.qq.dao.BaseDao;
import com.qq.dao.ChatrecordDao;

public class ChatrecordDaoImpl implements ChatrecordDao {

	@Override
	public int insertMessage(Chatrecord chatrecords) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "INSERT INTO chatrecords(my_accounts,friend_accounts,message,messageDate,state) "
				+ "VALUES (?,?,?,?,1)";
		
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, chatrecords.getMyAccount());	
			ps.setString(2, chatrecords.getFriendAccount());
			ps.setString(3,chatrecords.getMessage());
			ps.setObject(4, chatrecords.getMessageDate());
			result = ps.executeUpdate();
		} catch (SQLException e) {
			
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	@Override
	public int selectUserMesssageState(String myaccounts, String faccounts, int stateNum) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int rows = 0;
		String sql = "SELECT count(1) as total FROM chatrecords WHERE my_accounts=?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			if(res.next()) {
				rows = res.getInt("total");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return rows;
	}
	
	
	@Override
	public int selectUserMesssageState(String myaccounts, String faccounts) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int rows = 0;
		String sql = "SELECT COUNT(1) AS total FROM chatrecords WHERE my_accounts=? AND friend_accounts=? AND state=1";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			ps.setString(1, myaccounts);
			ps.setString(2, faccounts);
			if(res.next()) {
				rows = res.getInt("total");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return rows;

	}

	@Override
	public List<Chatrecord> selectMessageForMyfriend(String my_accounts, String friend_accounts) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Chatrecord> ctc = new ArrayList();
		String sql = "SELECT * FROM chatrecords WHERE (my_accounts = ? AND friend_accounts = ?) "
				+ "OR (my_accounts = ? AND friend_accounts = ?)";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, my_accounts);
			ps.setString(2, friend_accounts);
			ps.setString(3, friend_accounts);
			ps.setString(4, my_accounts);
			res = ps.executeQuery();
			while(res.next()) {
				Chatrecord ctr = new Chatrecord();
				ctr.setCharId(res.getInt("chat_id"));
				ctr.setMyAccount(res.getString("my_accounts"));
				ctr.setFriendAccount(res.getString("friend_accounts"));
				ctr.setMessage(res.getString("message"));
				ctr.setMessageDate(res.getDate("messageDate"));
				ctr.setState(res.getInt("state"));
				ctc.add(ctr);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return ctc;

	}
}
